package nl.hr.cmi.project4.inf1m.groep1.ZuinigRijden;
//commit line
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.location.Location;
import android.media.MediaPlayer;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ZuinigRijden extends Activity{
	
	
	private Button buttonRecordStartStop, buttonOpenRoute;
    private TextView longTextView;
    private TextView latTextView;
    private TextView snelheid;
    private LocationHolder locationHolder;
    private MediaPlayer buttonKnopSound;
    

    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        buttonKnopSound = MediaPlayer.create(this, R.raw.button);
        buttonRecordStartStop = (Button)findViewById(R.id.recordStartStop);
        buttonOpenRoute = (Button)findViewById(R.id.openRoute);
        snelheid = (TextView)findViewById(R.id.snelheid);
        snelheid.setText("0");
        
        //longTextView = (TextView)findViewById(R.id.longTextView);
        //latTextView = (TextView)findViewById(R.id.latTextView);
        
        locationHolder = new LocationHolder(this);
      
        
        buttonRecordStartStop.setOnClickListener(new startStopRecordingListener());
        buttonOpenRoute.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	buttonKnopSound.start();
            	getRoutes();
            }
    });     
    }//
    
    public void getRoutes(){
    	locationHolder.openRoute();
    }
    
    public void updateLocation(Location location){
        if(location != null){
        	int speed=(int) ((location.getSpeed()*60)*60);
            snelheid.setText(Integer.toString(speed));
        }
	}
    public void startRecording(){
    	locationHolder.startRecording();
        buttonRecordStartStop.setBackgroundResource(R.drawable.knop_stop);
    }
    
    public void stopRecording(){
    	locationHolder.stopRecording();
        buttonRecordStartStop.setBackgroundResource(R.drawable.knop_start);
    }
    
    private class startStopRecordingListener implements OnClickListener{
    	@Override
        public void onClick(View v) {
    		buttonKnopSound.start();
        	if(locationHolder.isRecording()){
        		AlertDialog.Builder builder = new AlertDialog.Builder(ZuinigRijden.this);
        		builder.setMessage("Are you sure you want to stop recording?")
        		       .setCancelable(false)
        		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        		           public void onClick(DialogInterface dialog, int id) {
        		        	   buttonKnopSound.start();
        		               stopRecording();//ZuinigRijden.this.getRoutes();
        		           }
        		       })
        		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
        		           public void onClick(DialogInterface dialog, int id) {
        		                dialog.cancel();
        		           }
        		       });
        		AlertDialog alert = builder.create();
        		alert.show();
        	} else {
        		startRecording();
        	}
        }
    }
    public ArrayList<SmallLocation> getLocationList(){
    	return locationHolder.getLocationList();
    }
}